package com.insurance.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.insurance.domain.bo.*;
import com.insurance.domain.entity.UserProduct;
import com.insurance.service.ChainConnectService;
import com.insurance.utils.CommonResponse;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;

@RestController
@CrossOrigin
@ResponseBody
public class ChainConnectController {

    @Resource
    private ChainConnectService chainConnectService;

    //采用椭圆曲线算法进行公私密钥分配;
    @RequestMapping("/TYQX")
    public CommonResponse TYQX(@RequestBody CPatient cPatient){
        JSONObject _obj= chainConnectService.TYQX(cPatient);
        if (_obj == null){
            return CommonResponse.fail("50001","患者注册失败，请重试");
        }
        return CommonResponse.ok(_obj);
    }

    //登录查询自己信息;
    @RequestMapping("/QueryPat")
    public CommonResponse QueryPat(@RequestBody CPatient cPatient){
        JSONArray _list= chainConnectService.QueryPat(cPatient);
        if (_list == null){
            return CommonResponse.fail("50001","患者注册失败，请重试");
        }
        Iterator<Object> iterator = _list.stream().iterator();
        while (iterator.hasNext()){
            cPatient.setAddress(String.valueOf(iterator.next()));
            cPatient.setName(String.valueOf(iterator.next()));
            cPatient.setSex(String.valueOf(iterator.next()));
            cPatient.setFactor(String.valueOf(iterator.next()));
            cPatient.setMessage(String.valueOf(iterator.next()));
            cPatient.setAge(String.valueOf(iterator.next()));
        }
        return CommonResponse.ok(cPatient);
    }

        //查询自己所有电子病历信息;
    @RequestMapping("/QueryAllEvi")
    public CommonResponse QueryAllEvi(@RequestBody CPatient cPatient){
        JSONArray _obj= chainConnectService.QueryAllEvi(cPatient);
        if (_obj == null){
            return CommonResponse.fail("50001","患者注册失败，请重试");
        }
        Case case1 = new Case();
        Iterator<Object> iterator = _obj.stream().iterator();
        while (iterator.hasNext()){
            case1.setCaseId(String.valueOf(iterator.next()));
            case1.setMedical(String.valueOf(iterator.next()));
            case1.setAdvice(String.valueOf(iterator.next()));
            case1.setSymptoms(String.valueOf(iterator.next()));
            case1.setTemp(String.valueOf(iterator.next()));
            case1.setHospital(String.valueOf(iterator.next()));
        }
        return CommonResponse.ok(case1);
    }

    //查询所以用户看看他们有没有什么可以进行共享操作;
    @RequestMapping("/QueryAllUsers")
    public CommonResponse QueryAllUsers(){
        JSONArray _obj= chainConnectService.QueryAllUsers();
        if (_obj == null){
            return CommonResponse.fail("50001","患者注册失败，请重试");
        }
        ArrayList<CPatient> patients = new ArrayList<>();
        Iterator<Object> iterator = _obj.stream().iterator();
        while (iterator.hasNext()){
            JSONArray next = (JSONArray) iterator.next();
            Iterator<Object> iterator1 = next.stream().iterator();
            while (iterator1.hasNext()){
                CPatient cPatient = new CPatient();
                cPatient.setAddress(String.valueOf(iterator1.next()));
                cPatient.setPirvateKey(String.valueOf(iterator1.next()));
                cPatient.setName(String.valueOf(iterator1.next()));
                cPatient.setAge(String.valueOf(iterator1.next()));
                cPatient.setSex(String.valueOf(iterator1.next()));
                cPatient.setMessage(String.valueOf(iterator1.next()));
                patients.add(cPatient);
            }
        }
        return CommonResponse.ok(patients);
    }

    //书写描述书信息，看看自己需要什么电子病历;
    @RequestMapping("/WriteMessaage")
    public CommonResponse WriteMessaage(@RequestBody CPatient cPatient){
        JSONObject _obj= chainConnectService.WriteMessaage(cPatient);
        if (_obj == null){
            return CommonResponse.fail("50001","患者注册失败，请重试");
        }
        return CommonResponse.ok(_obj);
    }

    //扫码进行公钥入栈操作;
    //医生用户查看
    @RequestMapping("/Verification")
    public CommonResponse Verification(@RequestBody CDoctor cDoctor){
        JSONObject _obj= chainConnectService.Verification(cDoctor);
        if (_obj == null){
            return CommonResponse.fail("50001","患者注册失败，请重试");
        }
        return CommonResponse.ok(_obj);
    }

    //医生查询自己患者下的电子病历;
    @RequestMapping("/QueryPeople")
    public CommonResponse QueryPeople(){
        CDoctor cDoctor = new CDoctor();
        cDoctor.setDocAdd("doctorAdress");
        JSONArray _obj= chainConnectService.QueryPeople(cDoctor);
        if (_obj == null){
            return CommonResponse.fail("50001","患者注册失败，请重试");
        }
        return CommonResponse.ok(_obj);
    }

    //医生书写该电子病历(一个用户对应一张病历);
    @RequestMapping("/WrtieEviCode")
    public CommonResponse WrtieEviCode(@RequestBody WrtieEviCode wrtieEviCode){
        JSONObject _obj= chainConnectService.WrtieEviCode(wrtieEviCode);
        if (_obj == null){
            return CommonResponse.fail("50001","患者注册失败，请重试");
        }
        return CommonResponse.ok(_obj);
    }

    //患者出院;
     @RequestMapping("/OutHospital")
    public CommonResponse OutHospital(@RequestBody CDoctor cDoctor){
        JSONObject _obj= chainConnectService.OutHospital(cDoctor);
        if (_obj == null){
            return CommonResponse.fail("50001","患者注册失败，请重试");
        }
        return CommonResponse.ok(_obj);
    }
    //
    @RequestMapping("/SetXiEvi")
    public CommonResponse SetXiEvi(@RequestBody SharedRecord sharedRecord){
            JSONObject _obj= chainConnectService.SetXiEvi(sharedRecord);
        if (_obj == null){
            return CommonResponse.fail("50001","患者注册失败，请重试");
        }
        return CommonResponse.ok(_obj);
    }



    //查询自己可以查看的电子病历;（C级权限，查询别人共享给我的病历，只能查询）
    @RequestMapping("/QueryGX")
    public CommonResponse QueryGX(@RequestBody SharedRecord sharedRecord){
        ArrayList<Case> _obj= chainConnectService.QueryGX(sharedRecord);
        if (_obj == null){
            return CommonResponse.fail("50001","患者注册失败，请重试");
        }
        return CommonResponse.ok(_obj);
    }

    //查询自己可以管理的电子病历;（查询我可以管理的电子病历）
    @RequestMapping("/QueryGL")
    public CommonResponse QueryGL(@RequestBody SharedRecord sharedRecord){
        ArrayList<Case> _obj= chainConnectService.QueryGL(sharedRecord);
        if (_obj == null){
            return CommonResponse.fail("50001","患者注册失败，请重试");
        }
        return CommonResponse.ok(_obj);
    }

    //共享管理
    //先进行QueryGl(查询我能管理的电子病历)这个操作，可以进行查找我能进行赋予权限给别人的电子病历；
    //然后再选择这里面的某张电子病历，选择某个用户进行共享
    //所以第一个参数是我所能赋予权限的病历ID，第二个就是用户的ID;
    @RequestMapping("/GXGL")
    public CommonResponse GXGL(@RequestBody GXGL gxgl){
        JSONObject _obj= chainConnectService.GXGL(gxgl);
        if (_obj == null){
            return CommonResponse.fail("50001","患者注册失败，请重试");
        }
        return CommonResponse.ok(_obj);
    }

    //查看公共账本上的
    @RequestMapping("/QueryBlockChain")
    public CommonResponse QueryBlockChain(){
        ArrayList<Case> _obj= chainConnectService.QueryBlockChain();
        if (_obj == null){
            return CommonResponse.fail("50001","患者注册失败，请重试");
        }
        return CommonResponse.ok(_obj);
    }

    /**
     * 购买保险产品，传入参数为当前用户id和所购买保险产品，并传入用户和保险产品之间符合的约束条件
     * @param userProduct
     * @return
     */
    @RequestMapping("/buyProduct")
    public CommonResponse BuyProduct(@RequestBody UserProduct userProduct){
        System.out.println(userProduct);
        JSONObject _obj = chainConnectService.BuyInsurance(userProduct);
        if (_obj == null){
            return CommonResponse.fail("50001","购买失败，请稍后重试");
        }
        return CommonResponse.ok(_obj);
    }

    @RequestMapping("/compensate")
    public CommonResponse Compensate(@RequestBody UserProduct userProduct){
        JSONArray objects = chainConnectService.AutoMoney(userProduct);
        if (objects == null){
            return CommonResponse.fail("50001","索赔失败，请稍后重试");
        }
        return CommonResponse.ok(objects);
    }

}
